/**
 * @class   CommunicationManagerImpl
 * @brief   
 *
 *
 *
 *
 *
 ****************************************************************************
 * @version $Id: CommunicationManagerImpl.h 2488 2010-05-21 01:48:27Z alex $
 * @author  Alessandro Polo
 ****************************************************************************/
/* Copyright (c) 2007-2010, WOSH - Wide Open Smart Home 
 * by Alessandro Polo - OpenSmartHome.com
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the OpenSmartHome.com WOSH nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY Alessandro Polo ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL Alessandro Polo BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 ****************************************************************************/

#ifndef __WOSH_Services_CommunicationManagerImpl_H__
 #define __WOSH_Services_CommunicationManagerImpl_H__

 #include <core/BundleGenericWorker.h>
 #include <core/Containers.h>
 #include <core/User.h>
 #include <core/MessageQueue.h>
 #include <core/Notification.h>
 #include <framework/communication/NotificationFlow.h>
 #include <framework/communication/NotificationConversion.h>

 #include <core/Communicator.h>
 #include <core/Interpreter.h>

 #include <interfaces/services/CommunicatorService.h>
 #include <interfaces/services/InterpreterService.h>

 #include "ICommunicationManagerListener.h"


using namespace wosh;
using namespace wosh::communication;


class CommunicationManagerImpl : public BundleGenericWorker  {

	public:

	public:

		/**
		 * @brief   Default constructor. Init vars.
		 * @param bundle [in] the parent bundle, see BundleGenericWorker interface
		 */
		CommunicationManagerImpl( BundleGeneric& bundle );

		/**
		 * @brief   Deconstructor. If thread is stll running, it will be stopped.
		 */
		virtual ~CommunicationManagerImpl();


	public:

		void evalMessage( const Message& message );

		NotificationFlow* evalNotification_( const Notification* notification, const Message* message );

		bool sendNotification_( NotificationFlow* notification );

		void archiveNotification_( NotificationFlow* notification );

	protected:

		void communicator_reply_sent( WRESULT ret, long notification_id, const URI& source );
		void communicator_reply_read( WRESULT ret, long notification_id, const URI& source );


	protected: // in CommunicationManagerSelector.cpp

		WRESULT intersectCommunicators_( UserInfoCommmunicators* usrComms, const Notification* notification, const std::string& location );

		wosh::Interpreter* selectInterpreter_( const Notification* notification, const wosh::Communicator* comm );

		UserInfoCommmunicators* getCommunicatorsOf( const std::string& username );

		bool isInterpreterRequired_( const Notification* notification, const wosh::Communicator* comm );


/** @name Setters
 * @{
 ******************************************************************************/
	public:

		void setListener( ICommunicationManagerListener* list )	{ this->listener = list; }

		void setFlowTimeOut( long seconds )						{ this->flowTimeout = seconds; }
		void setFlowRetryTime( long seconds )					{ this->flowRetryTime = seconds; }
		void setFlowRetryCount( long times )					{ this->flowRetryCount = times; }

//@}
/** @name Getters
 * @{
 ******************************************************************************/
	public:
		long getFlowTimeOut() const								{ return this->flowTimeout; }
		long getFlowRetryTime() const							{ return this->flowRetryTime; }
		long getFlowRetryCount() const							{ return this->flowRetryCount; }

		tStrCommunicatorMap& getCommunicators()					{ return this->comms; }
		tStrInterpreterMap& getInterpreters()					{ return this->transl; }

		MapT<long, NotificationFlow*>& getMessages()				{ return this->messages; }
		MapT<long, NotificationFlow*>& getMessagesArchived()		{ return this->messagesArchived; }

//@}
/** @name Internal methods
 * @{
 ******************************************************************************/
	protected:

		bool processNotification_( NotificationFlow* notification );

		/**
		 * @brief   
		 * @see     running
		 * @see     
		 */
		virtual void runThread();

//@}
	protected:

		tStrCommunicatorMap comms;			///< communicators
		tStrInterpreterMap transl;			///< interpreters

		MapT<long, NotificationFlow*> messages;			///< processing messages cache [flowid, notf*]
		MapT<long, NotificationFlow*> messagesArchived;	///< archived messages cache [flowid, notf*]
		long messageArchiveSize;						///< size of archived messages cache

		long flowTimeout;								///< timeout of notification-flow processing
		long flowRetryTime;								///< minimum number of seconds between each flow-processing retry [0=disabled]
		long flowRetryCount;							///< maximum number of retries of flow-processing [0=disabled]

		MessageQueue msgQueue;							///< messages' queue

		ICommunicationManagerListener* listener;		///< callbacks


}; // class def


#endif //__WOSH_Services_CommunicationManagerImpl_H__
